/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.srgsxmldb.database;

import java.util.ResourceBundle;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.cmd.Close;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.Open;

/**
 * Class providing safe functionality over baseX database
 * 
 * @author Eldred
 */
public class BaseXDBFactory {
 
    /**
     * open collection for specific context
     * @param context wanted context
     * @throws BaseXException 
     */
    public static void openCollection(Context context) throws BaseXException {
        
        if(context==null)
            throw new NullPointerException("context");

        ResourceBundle rb = ResourceBundle.getBundle("cz.muni.fi.pb138.srgsxmldb.properties/baseXdatabase");
        
        //new Open(rb.getString("collectionAddress")+rb.getString("collectionName")).execute(context);
        new Open(rb.getString("collectionName")).execute(context);
    }
    
    /**
     * create collection for specific context
     * @param context
     * @throws BaseXException 
     */
    public static void createCollection(Context context) throws BaseXException {
        
        if(context==null)
            throw new NullPointerException("context");
        
        ResourceBundle rb = ResourceBundle.getBundle("cz.muni.fi.pb138.srgsxmldb.properties/baseXdatabase");
        
        new CreateDB(rb.getString("databaseName"), rb.getString("databaseAddress")).execute(context);
    }
    
    /**
     * close collection in specific context
     * @param context
     * @throws BaseXException 
     */
    public static void closeCollection(Context context) throws BaseXException {
        
        if(context==null)
            throw new NullPointerException("context");
        
        new Close().execute(context);
    }
    
    /**
     * get absolute path for the folder with original xml files
     * @return absolute path for the folder with original xml files
     */
    public static String getDatabaseLocation() {
        
        ResourceBundle rb = ResourceBundle.getBundle("cz.muni.fi.pb138.srgsxmldb.properties/baseXdatabase");
        
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        //classLoader.getResource(rb.getString("databaseAddress"));
        
        return classLoader.getResource(rb.getString("databaseAddress")).toString();
    }
    
}
